package com.example.movie.vm

import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.bawei.common.entity.MovieVideoEntity
import com.example.common.vm.BaseViewModel
import com.example.movie.intent.MovieViewIntent
import com.example.movie.model.MoviePagingSource
import com.example.movie.model.MovieRepository
import com.example.movie.state.MovieUiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject

@HiltViewModel
class MovieViewModel @Inject constructor(private val repository:MovieRepository):BaseViewModel<MovieViewIntent>(){

    private val  _uiState = MutableStateFlow<MovieUiState>(MovieUiState.LoadEmpty)
    val uiState : StateFlow<MovieUiState> = _uiState.asStateFlow()




    override fun dispatchIntent(intent: MovieViewIntent) {
        when(intent){
            is MovieViewIntent.LoadLevelOneType -> loadLeverOneType()
            is MovieViewIntent.LoadLevelTwoType -> loadLeverTwoType(intent.pid)
        }
    }





    private fun loadLeverOneType(){
        viewModelScope.launch(Dispatchers.IO){
            try {
                withContext(Dispatchers.Main) {
                    val (code,data,msg)=repository.getMutilType()
                    if(code ==0 ){
                        _uiState.value = MovieUiState.LoadLeverOneTypeSuccess(data)
                    }else{
                        _uiState.value = MovieUiState.LoadLevelOneTypeFail("请求分类数据失败")
                    }
                }
            }catch (e:Exception){
                e.printStackTrace()
                _uiState.value = MovieUiState.LoadLevelOneTypeFail("分类异常"+e.message)
            }finally {
                cancel()
            }
        }
    }
    private fun loadLeverTwoType(pid:Int){
        viewModelScope.launch(Dispatchers.IO){
            try {
                withContext(Dispatchers.Main){
                    val (code,data,msg) = repository.getMutilTypeById(pid)
                    if(code == 0){
                        _uiState.value = MovieUiState.LoadLeverTwoTypeSuccess(data)
                    }else{
                        _uiState.value = MovieUiState.LoadLeverTwoTypeFail("二级分裂请求错误")
                    }
                }
            }catch (e:Exception){
                e.printStackTrace()
                _uiState.value = MovieUiState.LoadLeverTwoTypeFail("二级分类请求异常"+e.message)
            }finally {
                cancel()
            }
        }



    }
    fun loadRecommendVideo(): Flow<PagingData<MovieVideoEntity>> {
        return Pager(config = PagingConfig(initialLoadSize = 1, pageSize = 10, enablePlaceholders = false), pagingSourceFactory = { MoviePagingSource() }).flow.cachedIn(viewModelScope)
    }


    /**
     * 获取分类电影列表
     */
    fun loadMutilVideoById(typeId: Int): Flow<PagingData<MovieVideoEntity>> {
        return Pager(config = PagingConfig(initialLoadSize = 1, pageSize = 10, enablePlaceholders = false), pagingSourceFactory = { MoviePagingSource(typeId)}).flow.cachedIn(viewModelScope)
    }



}